package com.cspg.ssss.service;

import com.cspg.ssss.controller.RectifyController;
import com.cspg.ssss.domain.CompanyRectifyViewEntity;
import com.cspg.ssss.domain.DeptRectifyViewEntity;
import com.cspg.ssss.domain.RectifyHistoryEntity;
import com.cspg.ssss.domain.SafeDeptMap;
import com.cspg.ssss.repository.CompanyRectifyEntityMapper;
import com.cspg.ssss.repository.DeptRectifyEntityMapper;
import com.cspg.ssss.repository.RectifyHistoryEntityMapper;
import com.cspg.ssss.repository.SafeDeptMapMapper;
import com.cspg.ssss.vo.ProblemGradeViewVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 问题整改服务
 *
 *  @author xiongzq
 *  @since 2020-10-20
 */
@Service
public class RectifyService {

    private static Logger log = LoggerFactory.getLogger(RectifyService.class.getName());

    @Autowired
    SafeDeptMapMapper safeDeptMapMapper;

    @Autowired
    RectifyHistoryEntityMapper rectifyHistoryEntityMapper;

    @Autowired
    CompanyRectifyEntityMapper companyRectifyEntityMapper;

    @Autowired
    DeptRectifyEntityMapper deptRectifyEntityMapper;

    /**
     * 根据单位名称获取安监部门全路径
     * @param corp 例如 广州局
     * @return 返回 超高压输电公司/广州局/安全监管部
     */
    public String getSafeDeptByCompany(String corp){

        List<SafeDeptMap> ls = safeDeptMapMapper.selectDeptNameForCompany(corp);
        if(ls != null && ls.size() == 1){
            return "超高压输电公司/"+ls.get(0).getCompany()+"/"+ls.get(0).getSafe_dept();
        }

        return null;
    }

    public PageInfo<CompanyRectifyViewEntity> getCompanyRectifyListByCondition(int pageNum, int pageSize, Map<String,Object> condition){
        PageHelper.startPage(pageNum, pageSize);
        List<CompanyRectifyViewEntity> retlist = new ArrayList<CompanyRectifyViewEntity>();
        retlist = companyRectifyEntityMapper.selectCompanyRectify(condition);
        PageInfo<CompanyRectifyViewEntity> pgInfo = new PageInfo<CompanyRectifyViewEntity>(retlist);
        return pgInfo;
    }

    public PageInfo<DeptRectifyViewEntity> getDeptRectifyListByCondition(int pageNum, int pageSize,Map<String,Object> condition){
        PageHelper.startPage(pageNum, pageSize);
        List<DeptRectifyViewEntity> retlist = new ArrayList<DeptRectifyViewEntity>();
        retlist = deptRectifyEntityMapper.selectDeptRectify(condition);
        PageInfo<DeptRectifyViewEntity> pgInfo = new PageInfo<DeptRectifyViewEntity>(retlist);
        return pgInfo;
    }

    public List<CompanyRectifyViewEntity> getCompanyRectifyListByConditionNoPage(Map<String,Object> condition){
        List<CompanyRectifyViewEntity> retlist = new ArrayList<CompanyRectifyViewEntity>();
        retlist = companyRectifyEntityMapper.selectCompanyRectify(condition);

        return retlist;
    }

    public List<DeptRectifyViewEntity> getDeptRectifyListByConditionNoPage(Map<String,Object> condition){
        List<DeptRectifyViewEntity> retlist = new ArrayList<DeptRectifyViewEntity>();
        retlist = deptRectifyEntityMapper.selectDeptRectify(condition);
        return retlist;
    }

    public void updateCompanyRectify(Map<String,Object> condition){
        companyRectifyEntityMapper.updateCompanyRectify(condition);
    }

    public void insertCompanyRectify(Map<String,Object> condition){
        companyRectifyEntityMapper.insertCompanyRectify(condition);
    }

    public void insertDeptRectify(Map<String,Object> condition){
        deptRectifyEntityMapper.insertDeptRectify(condition);
    }

    public void updateDeptRectify(Map<String,Object> condition){
        deptRectifyEntityMapper.updateDeptRectify(condition);
    }


    /**
     * 获取某个问题的跟踪整改历史
     * @param problemid
     * @return
     */
    public List<RectifyHistoryEntity> getRectifyHistory(String problemid){
        Map<String,Object> condition = new HashMap<String, Object>();
        condition.put("problem_id",problemid);

        return rectifyHistoryEntityMapper.selectRectifyHistory(condition);
    }

    /**
     * 批量添加历史
     * @param enls
     */
    public void batchAddHistory(List<RectifyHistoryEntity> enls){

        rectifyHistoryEntityMapper.batchInsertRectifyHistory(enls);
    }

    public void insertRectifyHistory(RectifyHistoryEntity en){
        rectifyHistoryEntityMapper.insertRectifyHistory(en);
    }

    /**
     * 批量审批服务
     * @param condition
     */
    @Transactional
    public void batchRectifyApprove(Map<String,Object> condition){
        condition.put("op_datetime",new Date());
        String rectifyType = (String) condition.get("rectify_type");
        if(rectifyType.equals("company")){
            rectifyHistoryEntityMapper.batchInsertRectifyApproveHistory(condition);
            companyRectifyEntityMapper.batchUpdateCompanyRectify(condition);

        }
        if(rectifyType.equals("dept")){
            String newRectifyStatus = (String) condition.get("new_rectify_status");
            if(newRectifyStatus.equals("approve")){
                condition.put("flow_type",0);
                condition.put("rectify_status","rectify");
                rectifyHistoryEntityMapper.batchInsertRectifyApproveHistory(condition);
                deptRectifyEntityMapper.batchUpdateDeptRectify(condition);


                condition.put("flow_type",1);
                condition.put("rectify_status","manage_approve");
                rectifyHistoryEntityMapper.batchInsertRectifyApproveHistory(condition);
                deptRectifyEntityMapper.batchUpdateDeptRectify(condition);


            }else if(newRectifyStatus.equals("complete")){
                condition.put("flow_type",0);
                condition.put("rectify_status","commit");
                rectifyHistoryEntityMapper.batchInsertRectifyApproveHistory(condition);
                deptRectifyEntityMapper.batchUpdateDeptRectify(condition);


                condition.put("flow_type",1);
                condition.put("rectify_status","manage_complete");
                rectifyHistoryEntityMapper.batchInsertRectifyApproveHistory(condition);
                deptRectifyEntityMapper.batchUpdateDeptRectify(condition);

            }else{
                rectifyHistoryEntityMapper.batchInsertRectifyApproveHistory(condition);
                deptRectifyEntityMapper.batchUpdateDeptRectify(condition);

            }

        }
    }
}
